/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.base.config.exception;

import io.iec.edp.caf.commons.exception.config.ExceptionHandlingSettings;
import io.iec.edp.caf.commons.exception.entity.ExceptionHandlerInfo;
import io.iec.edp.caf.commons.exception.entity.ExceptionPolicyInfo;
import io.iec.edp.caf.commons.exception.service.CafExceptionHandler;
import io.iec.edp.caf.commons.exception.service.ExceptionHandling;
import io.iec.edp.caf.commons.runtime.redis.properties.CafRedisConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author wangyandong
 * @date 2019/11/13 9:41
 * @email wangyandong@inspur.com
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({
        ExceptionHandlingSettings.class, CafRedisConfiguration.class
})
public class ExceptionAutoConfiguration {

    @Bean
    public CafExceptionHandler exceptionHandling(ExceptionHandlingSettings settings, List<ExceptionHandlerInfo> handlerDatas,
                                                 List<ExceptionPolicyInfo> policyDatas) {
        return new CafExceptionHandler(settings, handlerDatas, policyDatas);
    }

    @Bean
    public ExceptionHandling oldExceptionHandling(ExceptionHandlingSettings settings, List<ExceptionHandlerInfo> handlerDatas,
                                                  List<ExceptionPolicyInfo> policyDatas) {
        return new ExceptionHandling(settings, handlerDatas, policyDatas);
    }

    @Bean
    public ExceptionHandlerInfo defaultExceptionHandlerData() {
        ExceptionHandlerInfo data = new ExceptionHandlerInfo();
        data.setName("default");
        data.setImplClassName("io.iec.edp.caf.commons.exception.handler.DefaultExceptionHandler");
        data.setDescription("default exception handler");
        return data;
    }

    @Bean
    public ExceptionPolicyInfo defaultExceptionPolicyData() {
        ExceptionPolicyInfo data = new ExceptionPolicyInfo();
        data.setName("default");
        data.setHandlerList("default");
        data.setBasePolicyName("");
        return data;
    }

}
